package com.biz.websocket.config;

import org.springframework.util.*;
import org.springframework.web.context.request.*;

import javax.servlet.http.*;
import javax.websocket.*;
import javax.websocket.server.*;
import java.util.*;

/**
 * @Description
 *  鉴权
 * @Author liuxi
 * @Date 2024/4/23
 **/
public class WebSocketServerConfig extends ServerEndpointConfig.Configurator {

    @Override
    public boolean checkOrigin(String originHeaderValue) {
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        // 校验逻辑
        return true;
    }

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        Map<String, List<String>> parameterMap = request.getParameterMap();
        List<String> erpList = parameterMap.getOrDefault("erp", null);
        if (!CollectionUtils.isEmpty(erpList)) {
            sec.getUserProperties().put("erp", erpList.get(0));
        }
    }
}
